package com.lhl.fw.db.manager;

import com.lhl.fw.db.repository.BaseJpaRepository;
import com.querydsl.core.types.OrderSpecifier;
import com.querydsl.core.types.Predicate;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import java.io.Serializable;
import java.util.List;
import java.util.Optional;

/**
 * @className: com.lhl.fw.db.manager.BaseManager
 * @description: Manager基类
 * @author: king
 * @date: 2021-02-04 10:34
 **/
@Api(tags = "BaseManager", produces = "BaseManager")
public abstract class BaseManager<T, ID extends Serializable, R extends BaseJpaRepository<T, ID>> {
    @Autowired
    protected R baseJpaRepository;
    public R getBaseJpaRepository() {
        return baseJpaRepository;
    }

    public Optional<List<T>> findAll() {
        return Optional.ofNullable(baseJpaRepository.findAll());
    }

    public Optional<List<T>> findAll(Sort sort) {
        return Optional.ofNullable(baseJpaRepository.findAll(sort));
    }

    public Optional<List<T>> findAllById(Iterable<ID> ids) {
        return Optional.ofNullable(baseJpaRepository.findAllById(ids));
    }

    public <S extends T> List<S> saveAll(Iterable<S> entities) {
        return baseJpaRepository.saveAll(entities);
    }

    public void flush() {
        baseJpaRepository.flush();
    }

    public <S extends T> S saveAndFlush(S entity) {
        return baseJpaRepository.saveAndFlush(entity);
    }

    public void deleteInBatch(Iterable<T> entities) {
        baseJpaRepository.deleteInBatch(entities);
    }

    public void deleteAllInBatch() {
        baseJpaRepository.deleteAllInBatch();
    }

    public T getOne(ID id) {
        return baseJpaRepository.getOne(id);
    }

    public <S extends T> List<S> findAll(Example<S> example) {
        return baseJpaRepository.findAll(example);
    }

    public <S extends T> List<S> findAll(Example<S> example, Sort sort) {
        return baseJpaRepository.findAll(example, sort);
    }

    public Page<T> findAll(Pageable pageable) {
        return baseJpaRepository.findAll(pageable);
    }

    public <S extends T> S save(S entity) {
        return baseJpaRepository.save(entity);
    }

    public Optional<T> findById(ID id) {
        return baseJpaRepository.findById(id);
    }

    public boolean existsById(ID id) {
        return baseJpaRepository.existsById(id);
    }

    public long count() {
        return baseJpaRepository.count();
    }

    public void deleteById(ID id) {
        baseJpaRepository.deleteById(id);
    }

    public void delete(T entity) {
        baseJpaRepository.delete(entity);
    }

    public void deleteAll(Iterable<? extends T> entities) {
        baseJpaRepository.deleteAll(entities);
    }

    public void deleteAll() {
        baseJpaRepository.deleteAll();
    }

    public Page<T> findAll(Predicate predicate, Pageable pageable, OrderSpecifier<?>... orders) {
        return baseJpaRepository.findAll(predicate, pageable, orders);
    }
}
